//
//  CQLoginViewController.m
//  HiChat
//
//  Created by dllo on 16/1/15.
//  Copyright © 2016年 wqiong. All rights reserved.
//

#import "CQLoginViewController.h"
#import "CQLoginView.h"


@interface CQLoginViewController ()
@property (nonatomic, retain) CQLoginView *accountNumV;
@property (nonatomic, retain) CQLoginView *passWordV;
@end

@implementation CQLoginViewController
- (void)dealloc
{
    [_accountNumV release];
    [_passWordV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"注册新用户";
    
    UIButton *returnBut = [UIButton buttonWithType:UIButtonTypeCustom];
    returnBut.frame = CGRectMake(80, 300, 80, 50);
    [returnBut setTitle:@"返回" forState:UIControlStateNormal];
    [returnBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    returnBut.layer.cornerRadius = 10.0;
    returnBut.layer.borderWidth = 0.5;
    [returnBut addTarget:self action:@selector(returnButAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:returnBut];
    
    UIButton *sendBut = [UIButton buttonWithType:UIButtonTypeCustom];
    sendBut.frame = CGRectMake(self.view.bounds.size.width - 160, 300, 80, 50);
    [sendBut setTitle:@"发送" forState:UIControlStateNormal];
    [sendBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    sendBut.layer.cornerRadius = 10.0;
    sendBut.layer.borderWidth = 0.5;
    [sendBut addTarget:self action:@selector(sendButAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sendBut];
    
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStyleDone target:self action:@selector(rightAction)];
    
    self.accountNumV = [[CQLoginView alloc] initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, 70)];
    self.accountNumV.Lab.text = @"账   号:";
    self.accountNumV.textf.placeholder = @"请输入用于登录的唯一用户名，可以是汉字";
    [self.view addSubview:self.accountNumV];
    [_accountNumV release];
    
    self.passWordV = [[CQLoginView alloc] initWithFrame:CGRectMake(self.accountNumV.frame.origin.x, self.accountNumV.frame.origin.y + self.accountNumV.frame.size.height + 10, self.accountNumV.frame.size.width, self.accountNumV.frame.size.height)];
    self.passWordV.Lab.text = @"密   码:";
    self.passWordV.textf.placeholder = @"请输入用于登录的密码";
    [self.view addSubview:self.passWordV];
    [_passWordV release];
}
- (void)returnButAction {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)sendButAction {
    if (self.accountNumV.textf.text.length == 0 || self.passWordV.textf.text.length == 0) {
        UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"warning" message:@"用户名或密码不能为空" delegate:self cancelButtonTitle:@"Sure" otherButtonTitles:nil, nil];
        [alert show];
    } else {
        [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:self.accountNumV.textf.text password:self.passWordV.textf.text withCompletion:^(NSString *username, NSString *password, EMError *error) {
        
            if (!error) {
                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"通知" message:@"注册成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
                
                [self.navigationController popViewControllerAnimated:YES];
                
            }else{
                NSString *message = [NSString stringWithFormat:@"%@", error];
                UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"warning" message:message delegate:self cancelButtonTitle:@"Sure" otherButtonTitles:nil, nil];
                [alert show];
            }
            
        } onQueue:nil];

    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
